package com.shengde.jinwawaforleader.model.homePage;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProSchoolMiens;
import com.shengde.jinwawaforleader.utils.UmengShareUtil;
import com.shengde.jinwawaforleader.views.SchoolMienPopupWindow;
import com.umeng.socialize.UMShareAPI;

/**
 * 校园风采的
 * Created by JYQ on 2017/1/6 0006.
 */
public class SchoolMienActivity extends BaseActivity {
    private static final String TAG = "SchoolMienActivity";
    public WebView show_wv;
    private TextView tv_back; //
    private TextView tv_title;
    private TextView fill_add; //
    private LinearLayout loading_main;  //加载中..
    private SchoolMienPopupWindow schoolMienPopupWindow;
    private ProSchoolMiens.SitelistEntity sitelistEntities;
    private String sname;
    private String remark;
    private String img;
    private String url;

    @Override
    public void onRoot(Bundle savedInstanceState) {
        setContentView(R.layout.activity_school_mien);

    }

    @Override
    public void initView() {
        loading_main = (LinearLayout) findViewById(R.id.loading_main);

        tv_back = (TextView) findViewById(R.id.tv_back);
        tv_title = (TextView) findViewById(R.id.tv_title);
        fill_add = (TextView) findViewById(R.id.fill_add);
        show_wv = (WebView) findViewById(R.id.show_wv);

    }

    @Override
    public void initData() {
        tv_title.setText(getString(R.string.school_mien_official_web));
        fill_add.setText(getString(R.string.editor));
        sitelistEntities = (ProSchoolMiens.SitelistEntity) getIntent().getSerializableExtra("proSchoolMiens");
        WebSettings settings = show_wv.getSettings();
        settings.setJavaScriptEnabled(true);//支持script，可以分页显示等功能
        settings.setDomStorageEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);// 设置缓存模式：不使用缓存
        settings.setBuiltInZoomControls(true);//在可缩放页面，支持放大缩小功能
        settings.setUseWideViewPort(true);//加载可缩放页面时，双击缩放
        settings.setSupportZoom(true);
        settings.setDisplayZoomControls(false);
        settings.setLoadWithOverviewMode(true);
        // 设置4.2以后版本支持autoPlay，非用户手势促发
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            settings.setMediaPlaybackRequiresUserGesture(false);
        }
        if (sitelistEntities == null) {
            loading_main.setVisibility(View.GONE);
            Toast.makeText(SchoolMienActivity.this, "还没有校园风采呦,请去添加", Toast.LENGTH_SHORT).show();
        } else {
            showWebView();
        }
    }

    /**
     * 加载webview
     */
    private void showWebView() {
        show_wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                Log.i(TAG, "onPageFinished: 111111");
                loading_main.setVisibility(View.GONE);
            }

        });
        if (sitelistEntities == null) {
            return;
        }
        if ("1".equals(sitelistEntities.type)) {
            show_wv.loadUrl(sitelistEntities.url);
            Log.i(TAG, "showWebView: url = " + sitelistEntities.url);
        } else if ("2".equals(sitelistEntities.type)) {
            show_wv.loadUrl(BaseProtocol.SCHOOL_MIEN + User.getSchoolId(this));
        }
    }

    @Override
    public void initListener() {
        tv_back.setOnClickListener(this);
        fill_add.setOnClickListener(this);

    }

    @Override
    public void onWidgetClick(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                finish();
                break;
            case R.id.fill_add:
                showPopBottom(view);
                break;

        }
    }

    private void showPopBottom(View view) {
        schoolMienPopupWindow = new SchoolMienPopupWindow(this, itemsOnClick);
        //设置layout在PopupWindow中显示的位置
        schoolMienPopupWindow.showPopupWindow(view);
    }

    //为弹出窗口实现监听类
    private View.OnClickListener itemsOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            schoolMienPopupWindow.dismiss();
            switch (v.getId()) {
                case R.id.relative_share:
                    if (sitelistEntities == null) {
                        Toast.makeText(SchoolMienActivity.this, "请添加属于您的校园风采", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (sitelistEntities.sname == null) {
                        sname = "";
                    }
                    if (sitelistEntities.remark == null) {
                        remark = "";
                    }
                    if (sitelistEntities.img == null) {
                        img = "";
                    }

                    if ("1".equals(sitelistEntities.type)) {
                        if (sitelistEntities.url == null) {
                            url = "";
                            Toast.makeText(SchoolMienActivity.this, "分享链接不能为空", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        UmengShareUtil.shareUrl(SchoolMienActivity.this, sname, remark,
                                BaseProtocol.IMG_BASE + img, sitelistEntities.url);
                    } else if ("2".equals(sitelistEntities.type)) {
                        UmengShareUtil.shareUrl(SchoolMienActivity.this, sname, remark,
                                BaseProtocol.IMG_BASE + img, BaseProtocol.SCHOOL_MIEN + User.getSchoolId(SchoolMienActivity.this));
                    }
                    break;
                case R.id.relative_editor_link:
//                    Intent intent = new Intent(SchoolMienActivity.this, EditNewThreeLinkActivity.class);
                    Intent intent = new Intent(SchoolMienActivity.this, EditThreeLinkActivity.class);
                    intent.putExtra("sitelistEntities", sitelistEntities);
                    intent.putExtra("type", "1");
                    startActivity(intent);
                    break;
                case R.id.relative_editor_custom:
                    intent = new Intent(SchoolMienActivity.this, EditCustomActivity.class);
                    intent.putExtra("sitelistEntities", sitelistEntities);
                    startActivity(intent);
                    break;
            }
        }
    };
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        show_wv.stopLoading();
        show_wv.destroy();
    }
}
